package com.docjohnson.pamphlet.activities;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import com.docjohnson.pamphlet.R;

public class ProductDetailsActivity extends DefaultActivity {

    ProductDetailsActivity _this = this;
    WebView mProductWebView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.productdetails);

        init();
    }

    @SuppressLint("NewApi")
	private void init(){
        String productId = getIntent().getExtras().getString("productId");
        Button closeProductButton = (Button) findViewById(R.id.closeproductbuttonId);
        closeProductButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mProductWebView = (WebView)findViewById(R.id.productDetailsWebViewId);
        String p = "file:///" + Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pamphlets/" + "prodpage" + productId + ".html";
        mProductWebView.loadUrl(p);
        mProductWebView.setBackgroundColor(Color.TRANSPARENT);
        mProductWebView.getSettings().setAllowFileAccess(true);
        mProductWebView.getSettings().setJavaScriptEnabled(true);
        mProductWebView.getSettings().setSaveFormData(true);
        mProductWebView.getSettings().setBuiltInZoomControls(true);
        mProductWebView.addJavascriptInterface(new JavaScriptInterface(), "Android");
        mProductWebView.setWebViewClient(new PamphletWebViewClient());
        if(android.os.Build.VERSION.SDK_INT>=11){
        	mProductWebView.getSettings().setDisplayZoomControls(false);
        } else {
        	mProductWebView.getSettings().setBuiltInZoomControls(false);
		}
    }

    private class PamphletWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl (url);
            return true;
        }
    }

    public class JavaScriptInterface {

        /** Instantiate the interface and set the context */
        JavaScriptInterface() {
        }

        public void showVideo(String url) {
            startActivity(new Intent(_this, VideoViewActivity.class));
        }
    }

}
